﻿using Autofac;
using Autofac.Integration.Mvc;
using JinJun.YShow.BLL.IDenpendecyService;
using JinJun.YShow.DAL.IDenpendecy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;

namespace JinJun.YShow.Web.Dependcy
{
    public class AutofacConfig
    {
        public static void RegisterAutofac()
        {
            var builder = new ContainerBuilder();
            Type basetype = typeof(IDenpendecy);
            var assemblys = AppDomain.CurrentDomain.GetAssemblies().ToList();
            builder.RegisterAssemblyTypes(assemblys.ToArray())
                .Where(t => basetype.IsAssignableFrom(t) && t != basetype)
                .AsImplementedInterfaces().InstancePerLifetimeScope();

            Type repository = typeof(IDenpendecyRepository);
            builder.RegisterAssemblyTypes(assemblys.ToArray())
                .Where(t => repository.IsAssignableFrom(t) && t != repository)
                .AsImplementedInterfaces().InstancePerLifetimeScope();
            builder.RegisterControllers(assemblys.ToArray());
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

        }
    }
}